<?php
namespace app\api\model\goods;
use think\facade\Db;
use app\api\common;
class GoodsOrder{
	public $_user_id;
	public function insertOrderAddr($_data){
		return Db::name("shop_order_addr")->insert($_data);
	}
	
	public function insertOrder($_data)
	{
		common::delete("shop_order".$this->_user_id);
		return Db::name("shop_order")->insert($_data);
	}
	
	public function findUserOrder($_where,$_field){
		extract(common::get('shop_order'.$this->_user_id,[$_where,$_field,__function__]));
		if(empty($data)){
			$data=Db::name("shop_order")->where($_where)->field($_field)->find();
			common::set($data);
		}
		return $data;
	}
	public function findUserOrderNoCache($_where,$_field){
		return Db::name("shop_order")->where($_where)->field($_field)->find();
	}
	public function updateOrder($_up,$_where){
		common::delete("shop_order".$this->_user_id);
		return Db::name("shop_order")->where($_where)->update($_up);
	}
	
	public function insertOrderDetails($_data)
	{
		return Db::name("shop_order_details")->insert($_data);
	}
	
	public function updateOrderDetails($_up,$_where){
		return Db::name("shop_order_details")->where($_where)->update($_up);
	}
	
	public function selectOrderDetails($_where,$_field){
		$_data=Db::name("shop_order_details")->where($_where)->field($_field)->select();
		return empty($_data)?:$_data->toArray();
	}
	
	public function selectUserOrder($_where,$_field,$_limit,$_order)
	{
		extract(common::get('shop_order'.$this->_user_id,[$_where,$_field,$_limit,$_order,__function__]));
		if(empty($data)){
			$data=Db::name("shop_order")->where($_where)->field($_field)->limit(current($_limit),end($_limit))->order($_order)->select();
			common::set($data);
		}
		return empty($data)?:$data->toArray();
	}
	
	
	public function selectUserOrderDetails($_where,$_field)
	{
		$_data=Db::name("shop_order_details")->alias("o")
			->join("shop_goods g","g.id=o.goods_id")
			->where($_where)->field($_field)->select();
		return empty($_data)?:$_data->toArray();
	}
	
	public function selectOrderCouriderData($_where,$_field):array
	{
		$_data=Db::name("shop_order_courier")->where($_where)->field($_field)->select();	
		return empty($_data)?:$_data->toArray();
	}
	
	public function findOrderDetails($_where,$_field)
	{
		return Db::name("shop_order")->alias("o")
				->join("shop_order_addr addr","addr.id=o.addr_id")
				->leftJoin("shop_order_courier courier","courier.order_id=o.id")
				->leftJoin("shop_courier_company company","company.id=courier.company_id")
				->where($_where)
				->field($_field)
				->find();
		
	}
}
?>